package com.wc.算法提高课.C第三章_图论.二分图.机器任务;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/23 1:22
 * @description https://www.acwing.com/problem/content/378/
 */
public class Main {
    /**
     * 思路：转化为最小点覆盖问题 = 最大匹配问题<p>
     * ai = 0 || bi = 0一定会被优先做了<p>
     * 身下的ai bi作为一条无向边, 那么每条边至少得选一个点, 那么就符合了最小点覆盖的概念, 就可以等价于匈牙利算法
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110;
    static boolean[][] g = new boolean[N][N];
    static boolean[] st = new boolean[N];
    static int[] match = new int[N];
    static int n, m, k;

    public static void main(String[] args) {
        while ((n = sc.nextInt()) != 0) {
            m = sc.nextInt();
            for (int i = 1; i <= n; i++) Arrays.fill(g[i], false);
            Arrays.fill(match, 0);
            k = sc.nextInt();
            while (k-- > 0) {
                int t = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt();
                if (a == 0 || b == 0) continue;
                g[a][b] = true;
            }
            int res = 0;
            for (int i = 1; i < n; i++) {
                Arrays.fill(st, false);
                if (find(i)) res++;
            }
            out.println(res);
        }
        out.flush();
    }

    static boolean find(int u) {
        for (int i = 1; i < m; i++) {
            if (st[i] || !g[u][i]) continue;
            st[i] = true;
            int t = match[i];
            if (t == 0 || find(t)) {
                match[i] = u;
                return true;
            }
        }
        return false;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
